/**
 *  DBEXEL is a Database Backed & Web-Based Worksheet and chart management program. 
 *  It has been influenced by Excel.
 *  For questions or suggestions please contact the developper at ( Development@Gandomi.com )
 *  Copyright (C) 2011 Baubak Gandomi   
 *
 *	This file is part of the application DBEXEL
 *
 *   DBEXEL is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   DBEXEL is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package dbexel.presentation.aat;

import java.util.ArrayList;
import java.util.List;

import com.gargoylesoftware.htmlunit.ElementNotFoundException;
import com.gargoylesoftware.htmlunit.html.HtmlButtonInput;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLButtonElement;

/**
 * @author gandomi
 * 
 */
public class DBExelTestTools {
	private final static String ATTRIBUTE_ID = "id";
	private final static String ATTRIBUTE_NAME = "name";

	/**
	 * This method fetches all the visible input fields
	 * 
	 * @param in_htmlForm
	 *            the current HTMLPage
	 * @return a list of visible "<input type="text" fields
	 */
	public static List<HtmlInput> fetchVisibleInputFields(
			final HtmlForm in_htmlForm) {
		List<HtmlInput> lr_returnList = new ArrayList<HtmlInput>();

		for (HtmlElement lt_HtmlInput : in_htmlForm
				.getElementsByTagName("input")) {

			if (lt_HtmlInput instanceof HtmlTextInput) {
				lr_returnList.add((HtmlInput) lt_HtmlInput);

			}
		}
		// TODO Auto-generated method stub
		return lr_returnList;
	}

	/**
	 * This method returns all Input fields that start with a specific string
	 * 
	 * @param in_htmlForm
	 *            The HTMLForm containing input fields
	 * @param in_nameString
	 *            The prefix to look for
	 * @return
	 */
	public static List<HtmlInput> getInputsByNameStart(HtmlForm in_htmlForm,
			String in_nameString) {
		return getInputsByAttributeStartingWith(in_htmlForm, ATTRIBUTE_NAME,
				in_nameString);
	}

	/**
	 * This method returns all Input fields that start with a specific string
	 * 
	 * @param in_htmlForm
	 *            The HTMLForm containing input fields
	 * @param in_nameString
	 *            The prefix to look for
	 * @return
	 */
	public static List<HtmlInput> getInputsByIdStart(HtmlForm in_htmlForm,
			String in_nameString) {

		return getInputsByAttributeStartingWith(in_htmlForm, ATTRIBUTE_ID,
				in_nameString);
	}

	/**
	 * This method returns all Input fields that start with a specific string
	 * 
	 * @param in_htmlForm
	 *            The HTMLForm containing input fields
	 * @param in_AttributeType
	 *            this is the type of the attribute to look for. For now we only
	 *            have "name" and "input
	 * @param in_nameString
	 *            The prefix to look for
	 * @return
	 */
	private static List<HtmlInput> getInputsByAttributeStartingWith(
			HtmlForm in_htmlForm, String in_AttributeType, String in_nameString) {
		List<HtmlInput> lr_returnList = new ArrayList<HtmlInput>();

		for (HtmlElement lt_htmlInput : in_htmlForm
				.getElementsByTagName("input")) {

			if (lt_htmlInput.getAttribute(in_AttributeType).startsWith(
					in_nameString)) {
				lr_returnList.add((HtmlInput) lt_htmlInput);
			}
		}

		return lr_returnList;
	}

	/**
	 * This method returns all Select fields that start with a specific string
	 * 
	 * @param in_htmlForm
	 * @param in_nameString
	 * @return
	 */
	public static List<HtmlSelect> getSelectsByNameStart(HtmlForm in_htmlForm,
			String in_nameString) {
		List<HtmlSelect> lr_returnList = new ArrayList<HtmlSelect>();
		for (HtmlElement lt_htmlSelect : in_htmlForm
				.getElementsByTagName("select")) {

			if (lt_htmlSelect.getAttribute(DBExelTestTools.ATTRIBUTE_NAME)
					.startsWith(in_nameString)) {
				lr_returnList.add((HtmlSelect) lt_htmlSelect);
			}
		}

		return lr_returnList;
	}

	/**
	 * This method returns the submit button of a form. If more than one are
	 * found we return the first submit button
	 * 
	 * @param in_htmlForm
	 * @return a Submit button
	 */
	public static HtmlSubmitInput getSubmitButton(HtmlForm in_htmlForm) {
		// Parse all form input elements
		for (HtmlElement lt_htmlElement : in_htmlForm
				.getElementsByTagName("input")) {

			if (((HtmlInput) lt_htmlElement).getTypeAttribute()
					.equals("submit")) {
				return (HtmlSubmitInput) lt_htmlElement;
			}
		}

		throw new ElementNotFoundException("submit", "input", "");

	}

}
